// 自定义Promise
class KPromise {
  constructor(handle) {
    // Promise Status
    this.status = 'pending'
    this.value = undefined
    this.resolveFn = null
    this.rejectFn = null
    // 执行传入的函数
    handle(
      // resolve
      this._resolve.bind(this),
      // reject
      this._reject.bind(this)
    )
  }

  _resolve(value) {
    this.status = 'fullfilled'
    this.value = value
    setTimeout(() => {
      this.resolveFn(value)
    }, 0)
  }

  _reject(value) {
    this.status = 'rejected'
    this.value = value
    setTimeout(() => {
      this.rejectFn(value)
    }, 0)
  }
  // then
  then(onResolved, onRejected) {
    /**
     *  onResolved onRejected并不是在then里面执行的，他和resolve, reject相关的
     *
     * 所以需要保存函数，不执行
     *  */
    this.resolveFn = onResolved
    this.rejectFn = onRejected
  }
}
